#include "quickjs.h"
#include "qjs-libc.h"
void* get_current_task_handle();
int my_printf(const char* fmt, ...);

JSRuntime *rt;
JSContext *ctx;

void qjs_deinit(void)
{
  JS_FreeContext(ctx);
  JS_FreeRuntime(rt);  
}

void qjs_test(void)
{
  rt = NULL;
  ctx = NULL;
  my_printf("step %d ...\r\n", 1);
  rt = JS_NewRuntime();
  my_printf("step %p %d ...\r\n", rt, 2);

  ctx = JS_NewContextRaw(rt);
  if (!ctx)
      return;
  my_printf("step %p %d ...\r\n", ctx, 3);

  JS_AddIntrinsicBaseObjects(ctx);
  my_printf("step %d ...\r\n", 4);
  JS_AddIntrinsicDate(ctx);
  my_printf("step %d ...\r\n", 5);
  JS_AddIntrinsicEval(ctx);
  my_printf("step %d ...\r\n", 6);
  JS_AddIntrinsicStringNormalize(ctx);
  my_printf("step %d ...\r\n", 7);
  JS_AddIntrinsicRegExp(ctx);
  my_printf("step %d ...\r\n", 8);
  JS_AddIntrinsicJSON(ctx);
  my_printf("step %d ...\r\n", 9);
  JS_AddIntrinsicProxy(ctx);
  my_printf("step %d ...\r\n", 10);
  JS_AddIntrinsicMapSet(ctx);
  my_printf("step %d ...\r\n", 11);
  JS_AddIntrinsicTypedArrays(ctx);
  my_printf("step %d ...\r\n", 12);
  JS_AddIntrinsicPromise(ctx);
  my_printf("step %d ...\r\n", 13);

  js_std_add_helpers(ctx, 0, NULL);
  my_printf("step 4 ...\r\n");
}

void calc(char* code)
{
  JSValue rc = JS_Eval(ctx, code, strlen(code), "stdin.js", 0);
  my_printf("result %d\n", JS_VALUE_GET_INT(rc));
}

void info()
{
  my_printf("sizeof jsValue: %d\r\n", sizeof(JSValue));
  my_printf("current task: %p\r\n", get_current_task_handle());
}
